﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// TMS对账流水状态恢复
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMSShippingorder_BillUpdateController : ControllerBase
    {
        private readonly ILogger<TMSShippingorder_BillUpdateRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public TMSShippingorder_BillUpdateController(ILogger<TMSShippingorder_BillUpdateRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");

        }

        /// <summary>
        /// TMS对账流水状态恢复
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostTMSShippingorder_BillUpdate([FromBody] TMSShippingorder_BillUpdateRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMSShippingorder_BillUpdateResponseDto re = new TMSShippingorder_BillUpdateResponseDto();
            string dailyno = strparm.dailyno;
            string pj = "";

            try
            {
                strSql = new StringBuilder();
                strSql.Append(@"select * from  bill_daily_details   where dailyno='" + dailyno + "'");
                DataTable dt_daily = DB.Query(strSql.ToString(), con);



                if (dt_daily.Rows.Count> strparm.rows.Count)
                {
                    var row = strparm.rows;

                    foreach (var rows in row)
                    {
                        pj = pj + ";" + rows.statementno;
                    }

                    foreach (DataRow item in dt_daily.Rows)
                    {
                        if (pj.Contains(item["statementno"].ToString()))
                        {

                        }
                        else
                        {
                            strSql = new StringBuilder();
                            strSql.Append("update shippingorder_bill  set state=null  where statementno='"+ item["statementno"].ToString() + "'");
                            strSqlList.Add(strSql.ToString());
                        }
                    }

                    if (strSqlList.Count > 0)
                    {
                        bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                        if (A)
                        {

                        }
                        else
                        {
                            strMsg = "数据库更新失败，请联系管理员";
                        }
                    }
                }           

            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "0";
            }
            else
            {
                re.status = "失败";
                re.code = "500";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
